﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.MBSM.DBUtility;
public partial class Manage_GoodsAdd_ProductsSearch : System.Web.UI.Page
{
    private string _accountidadd = "";
    protected void Page_Load(object sender, EventArgs e)
    {

        if (HZ.MBSM.BLL.Login.CheckAssert(this.UseAccountType))
        {
            HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];
            _accountidadd = _Account.AccountId.ToString();
            if (!this.IsPostBack)
            {

            }
            if (_Account != null)
            {

                GridView1.Columns[8].Visible = true;
                GridView1.Columns[9].Visible = true;
                showGridView();
                GridView1.Columns[8].Visible = false;
                GridView1.Columns[9].Visible = false;


            }

        }
    }

    /// <summary>
    /// 将数据绑定到datalist
    /// </summary>
    public void showGridView()
    {

        string strConditions = fctConditions();
        Db db = new Db();
        DbPage dbPage = new DbPage(db);
        dbPage.Table = "product";                                               //要查询的表名
        dbPage.Key = "product.productid";                                               //表的主键    
        dbPage.Where = strConditions;                                           //查询条件    
        dbPage.SelectField = "isnull(IScompanyADD,0) as IScompanyADD,accountadd,product.productid,productcode,productname,medicinemodel,outlookc,unit,xishu,Convert(numeric(20,3),memo1) as memo1 ";//要显示的字段
        dbPage.PageIndex = SysFun.ToInt(Request["CurrentlyPageIndex"]);          //要显示第几页   
        dbPage.PageSize = 10;                                                    //没有显示记录条数   
        dbPage.OrderBy = "productname,medicinemodel,outlookc,product.ProductId";                  //排序字段
        System.Data.IDataReader dr = dbPage.ExecuteReader();
        GridView1.DataSource = dr;
        GridView1.DataBind();
        dr.Close();
        //dr.Dispose();
        dbPage.Db.Close();
        db.Close();
        dr = null;
        db = null;
        this.DbPageFlip1.DbPage = dbPage;//将数据传给控键

    }
    /// <summary>
    /// 生成查询条件
    /// </summary>
    /// <returns></returns>
    public string fctConditions()
    {
        string _strWhere;
        _strWhere = "";
        //品目号 改productcode为productid

        string _strProductCode = txtProductCode.Text.Trim();
        if (_strProductCode != "")
        {
            _strWhere = _strWhere + " product.Productid like '%" + _strProductCode + "%' and";
        }
        //通用名

        string _strProductName = txtProductName.Text.Trim();
        if (_strProductName != "")
        {
            _strWhere = _strWhere + " (ProductName like '%" + _strProductName + "%' or " + " SpellingCode like '%" + _strProductName + "%') and";
        }

        //剂型
        string _strMedicinemodel = txtMedicinemodel.Text.Trim();
        if (_strMedicinemodel != "")
        {
            _strWhere = _strWhere + " medicinemodel like '%" + _strMedicinemodel + "%' and";
        }

        //规格
        string _strOutlookc = txtOutlook.Text.Trim();
        if (_strOutlookc != "")
        {
            _strWhere = _strWhere + " outlookc like '%" + _strOutlookc + "%' and";
        }
        //单位
        string _strunit = txtunit.Text.Trim();
        if (_strunit != "")
        {
            _strWhere = _strWhere + " Unit like '%" + _strunit + "%' and";
        }

        if (_strWhere != "")
        {
            _strWhere = "  " + _strWhere.Substring(0, _strWhere.Length - 3);
        }
        return _strWhere;

    }
    //数据绑定
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.Cells[8].Text.Trim() == "1")//如果是增补的则不让再增补
            {
                if (e.Row.Cells[9].Text.Trim().Equals(_accountidadd))//如果是本企业自添加的目录只有自己可以删除。
                {

                }
                else
                {
                    e.Row.Cells[7].Text = "企业增补";
                }
            }
            else
            {
                e.Row.Cells[7].Text = "<a href=\"../company/ProductAddJBzb.aspx?ProductId=" + e.Row.Cells[0].Text + "\" target=\"_blank\">增补</a>";
            }
        }
    }

    #region IPageDescribe 成员


    public System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> UseAccountType
    {
        get
        {
            System.Collections.Generic.List<HZ.MBSM.DAL.AccountType> _AccountType = new System.Collections.Generic.List<HZ.MBSM.DAL.AccountType>();
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumAttorney);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumCheckGoods);
            _AccountType.Add(HZ.MBSM.DAL.AccountType.enumComapanyRG);
            return _AccountType;
        }
    }
    #endregion

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Del"))
        {
            string U_id = e.CommandArgument.ToString();

            int rtn = 0;
            try
            {
                HZ.MBSM.Model.Account _Account = (HZ.MBSM.Model.Account)Session["Account"];

                HZ.MBSM.BLL.Product productbll = new HZ.MBSM.BLL.Product();

                if (productbll.DEletePruduct(U_id))
                {
                    Response.Write("<script language=JavaScript>alert('删除成功！');</script>");
                    HZ.MBSM.BLL.LogManage.RecordLoginDetail(_Account.AccountName, Request.UserHostAddress, "删除", "product", "productid=" + U_id, "../Agency/UniteMange.aspx");
                }
                else
                {
                    Response.Write("<script language=JavaScript>alert('该目录下已经填报有产品，删除失败！');</script>");
                }
            }
            catch (Exception ae)
            {
                HZ.Web.WebUtil.Alert("数据异常请重试！", this);
            }
        }
    }
}

